

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/fluid.png">
  <link rel="icon" href="/img/fluid.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="ShanCe">
  <meta name="keywords" content="">
  
    <meta name="description" content="多sheet下载  https:&#x2F;&#x2F;easyexcel.opensource.alibaba.com&#x2F;docs&#x2F;current&#x2F;quickstart&#x2F;write  需要导出实体类123456789101112131415161718192021222324252627282930313233package com.hessianhealth.quality.entity;import com.al">
<meta property="og:type" content="article">
<meta property="og:title" content="java下载多sheet">
<meta property="og:url" content="https://sweetshance.github.io/2024/12/13/springBoot%E5%AD%A6%E4%B9%A0/java%E4%B8%8B%E8%BD%BD%E5%A4%9Asheet/index.html">
<meta property="og:site_name" content="ShanCe的博客">
<meta property="og:description" content="多sheet下载  https:&#x2F;&#x2F;easyexcel.opensource.alibaba.com&#x2F;docs&#x2F;current&#x2F;quickstart&#x2F;write  需要导出实体类123456789101112131415161718192021222324252627282930313233package com.hessianhealth.quality.entity;import com.al">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2024-12-13T07:16:56.000Z">
<meta property="article:modified_time" content="2024-12-13T09:28:01.856Z">
<meta property="article:author" content="ShanCe">
<meta property="article:tag" content="springboot">
<meta name="twitter:card" content="summary_large_image">
  
  
    <meta name="referrer" content="no-referrer-when-downgrade">
  
  
  <title>java下载多sheet - ShanCe的博客</title>

  <link  rel="stylesheet" href="https://lib.baomitu.com/twitter-bootstrap/4.6.1/css/bootstrap.min.css" />



  <link  rel="stylesheet" href="https://lib.baomitu.com/github-markdown-css/4.0.0/github-markdown.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/hint.css/2.7.0/hint.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.css" />



<!-- 主题依赖的图标库，不要自行修改 -->
<!-- Do not modify the link that theme dependent icons -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_hj8rtnfg7um.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/css/main.css" />


  <link id="highlight-css" rel="stylesheet" href="/css/highlight.css" />
  
    <link id="highlight-css-dark" rel="stylesheet" href="/css/highlight-dark.css" />
  




  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    Fluid.ctx = Object.assign({}, Fluid.ctx)
    var CONFIG = {"hostname":"sweetshance.github.io","root":"/","version":"1.9.4","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false,"scope":[]},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"left","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"code_language":{"enable":true,"default":"TEXT"},"copy_btn":true,"image_caption":{"enable":true},"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"placement":"right","headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"follow_dnt":true,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/local-search.xml"};

    if (CONFIG.web_analytics.follow_dnt) {
      var dntVal = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
      Fluid.ctx.dnt = dntVal && (dntVal.startsWith('1') || dntVal.startsWith('yes') || dntVal.startsWith('on'));
    }
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
  


  
<meta name="generator" content="Hexo 5.4.2"></head>


<body>
  

  <header>
    

<div class="header-inner" style="height: 70vh;">
  <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/">
      <strong>ShanCe</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                <span>首页</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                <span>归档</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                <span>分类</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                <span>标签</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                <span>关于</span>
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              <i class="iconfont icon-search"></i>
            </a>
          </li>
          
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">
              <i class="iconfont icon-dark" id="color-toggle-icon"></i>
            </a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

  

<div id="banner" class="banner" parallax=true
     style="background: url('/img/default.png') no-repeat center center; background-size: cover;">
  <div class="full-bg-img">
    <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
      <div class="banner-text text-center fade-in-up">
        <div class="h2">
          
            <span id="subtitle" data-typed-text="java下载多sheet"></span>
          
        </div>

        
          
  <div class="mt-3">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-author" aria-hidden="true"></i>
        ShanCe
      </span>
    
    
      <span class="post-meta">
        <i class="iconfont icon-date-fill" aria-hidden="true"></i>
        <time datetime="2024-12-13 15:16" pubdate>
          2024年12月13日 下午
        </time>
      </span>
    
  </div>

  <div class="mt-1">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-chart"></i>
        
          11k 字
        
      </span>
    

    
      <span class="post-meta mr-2">
        <i class="iconfont icon-clock-fill"></i>
        
        
        
          89 分钟
        
      </span>
    

    
    
      
        <span id="busuanzi_container_page_pv" style="display: none">
          <i class="iconfont icon-eye" aria-hidden="true"></i>
          <span id="busuanzi_value_page_pv"></span> 次
        </span>
        
      
    
  </div>


        
      </div>

      
    </div>
  </div>
</div>

</div>

  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="side-col d-none d-lg-block col-lg-2">
      

    </div>

    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">java下载多sheet</h1>
            
            
              <div class="markdown-body">
                
                <p>多sheet下载</p>
<blockquote>
<p><a target="_blank" rel="noopener" href="https://easyexcel.opensource.alibaba.com/docs/current/quickstart/write">https://easyexcel.opensource.alibaba.com/docs/current/quickstart/write</a></p>
</blockquote>
<h3 id="需要导出实体类"><a href="#需要导出实体类" class="headerlink" title="需要导出实体类"></a>需要导出实体类</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> com.hessianhealth.quality.entity;<br><br><span class="hljs-keyword">import</span> com.alibaba.excel.annotation.ExcelIgnore;<br><span class="hljs-keyword">import</span> com.alibaba.excel.annotation.ExcelProperty;<br><span class="hljs-keyword">import</span> com.baomidou.mybatisplus.annotation.TableId;<br><span class="hljs-keyword">import</span> com.baomidou.mybatisplus.annotation.TableName;<br><span class="hljs-keyword">import</span> lombok.Data;<br><br><span class="hljs-keyword">import</span> java.io.Serializable;<br><br><span class="hljs-comment">/*</span><br><span class="hljs-comment"> * @Description 下载测试类</span><br><span class="hljs-comment"> **/</span><br><span class="hljs-meta">@Data</span><br><span class="hljs-meta">@TableName(&quot;md_test&quot;)</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">DownloadTestEntity</span> <span class="hljs-keyword">implements</span> <span class="hljs-title class_">Serializable</span> &#123;<br>    <span class="hljs-meta">@TableId</span><br>    <span class="hljs-meta">@ExcelIgnore</span> <span class="hljs-comment">// 忽略改字段导出</span><br>    <span class="hljs-keyword">private</span> Integer id;<br><br>    <span class="hljs-meta">@ExcelProperty(&quot;用户名&quot;)</span> <span class="hljs-comment">// 字段head</span><br>    <span class="hljs-keyword">private</span> String username;<br><br>    <span class="hljs-meta">@ExcelProperty(&quot;姓名&quot;)</span><br>    <span class="hljs-keyword">private</span> String name;<br><br>    <span class="hljs-meta">@ExcelProperty(&quot;年龄&quot;)</span><br>    <span class="hljs-keyword">private</span> Integer age;<br><br>    <span class="hljs-meta">@ExcelProperty(&quot;性别&quot;)</span><br>    <span class="hljs-keyword">private</span> String gender;<br>&#125;<br><br></code></pre></td></tr></table></figure>

<h3 id="简单easyExcel-工具类"><a href="#简单easyExcel-工具类" class="headerlink" title="简单easyExcel 工具类"></a>简单easyExcel 工具类</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br><span class="line">220</span><br><span class="line">221</span><br><span class="line">222</span><br><span class="line">223</span><br><span class="line">224</span><br><span class="line">225</span><br><span class="line">226</span><br><span class="line">227</span><br><span class="line">228</span><br><span class="line">229</span><br><span class="line">230</span><br><span class="line">231</span><br><span class="line">232</span><br><span class="line">233</span><br><span class="line">234</span><br><span class="line">235</span><br><span class="line">236</span><br><span class="line">237</span><br><span class="line">238</span><br><span class="line">239</span><br><span class="line">240</span><br><span class="line">241</span><br><span class="line">242</span><br><span class="line">243</span><br><span class="line">244</span><br><span class="line">245</span><br><span class="line">246</span><br><span class="line">247</span><br><span class="line">248</span><br><span class="line">249</span><br><span class="line">250</span><br><span class="line">251</span><br><span class="line">252</span><br><span class="line">253</span><br><span class="line">254</span><br><span class="line">255</span><br><span class="line">256</span><br><span class="line">257</span><br><span class="line">258</span><br><span class="line">259</span><br><span class="line">260</span><br><span class="line">261</span><br><span class="line">262</span><br><span class="line">263</span><br><span class="line">264</span><br><span class="line">265</span><br><span class="line">266</span><br><span class="line">267</span><br><span class="line">268</span><br><span class="line">269</span><br><span class="line">270</span><br><span class="line">271</span><br><span class="line">272</span><br><span class="line">273</span><br><span class="line">274</span><br><span class="line">275</span><br><span class="line">276</span><br><span class="line">277</span><br><span class="line">278</span><br><span class="line">279</span><br><span class="line">280</span><br><span class="line">281</span><br><span class="line">282</span><br><span class="line">283</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> com.hessianhealth.quality.util;<br><br><span class="hljs-keyword">import</span> com.alibaba.excel.EasyExcel;<br><span class="hljs-keyword">import</span> com.alibaba.excel.ExcelReader;<br><span class="hljs-keyword">import</span> com.alibaba.excel.ExcelWriter;<br><span class="hljs-keyword">import</span> com.alibaba.excel.event.AnalysisEventListener;<br><span class="hljs-keyword">import</span> com.alibaba.excel.write.metadata.WriteSheet;<br><span class="hljs-keyword">import</span> com.alibaba.excel.write.metadata.style.WriteCellStyle;<br><span class="hljs-keyword">import</span> com.alibaba.excel.write.metadata.style.WriteFont;<br><span class="hljs-keyword">import</span> com.alibaba.excel.write.style.HorizontalCellStyleStrategy;<br><span class="hljs-keyword">import</span> com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy;<br><span class="hljs-keyword">import</span> com.alibaba.excel.write.style.row.SimpleRowHeightStyleStrategy;<br><span class="hljs-keyword">import</span> com.google.common.collect.Lists;<br><span class="hljs-keyword">import</span> com.hessianhealth.quality.config.Custemhandler;<br><span class="hljs-keyword">import</span> com.hessianhealth.quality.config.RowWriteHandlerImp;<br><span class="hljs-keyword">import</span> org.apache.poi.ss.formula.functions.T;<br><span class="hljs-keyword">import</span> org.apache.poi.ss.usermodel.*;<br><br><span class="hljs-keyword">import</span> javax.servlet.http.HttpServletResponse;<br><span class="hljs-keyword">import</span> java.io.*;<br><span class="hljs-keyword">import</span> java.net.URLEncoder;<br><span class="hljs-keyword">import</span> java.util.List;<br><br><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * <span class="hljs-doctag">@author</span> langz</span><br><span class="hljs-comment"> * <span class="hljs-doctag">@version</span> 1.0</span><br><span class="hljs-comment"> * <span class="hljs-doctag">@date</span> 2021/8/7 15:56</span><br><span class="hljs-comment"> */</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">EasyExcelUtils</span> &#123;<br><br>    <span class="hljs-keyword">private</span> <span class="hljs-title function_">EasyExcelUtils</span><span class="hljs-params">()</span>&#123;&#125;<br><br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 将列表以 Excel 响应给前端</span><br><span class="hljs-comment">     *</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> response 响应</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> filename 文件名</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> sheetName Excel sheet 名</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> head Excel head 头</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> data 数据列表哦</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> &lt;T&gt; 泛型，保证 head 和 data 类型的一致性</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@throws</span> IOException 写入失败的情况</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> &lt;T&gt; <span class="hljs-keyword">void</span> <span class="hljs-title function_">write</span><span class="hljs-params">(HttpServletResponse response, String filename, String sheetName,</span><br><span class="hljs-params">                                 Class&lt;T&gt; head, List&lt;T&gt; data)</span> <span class="hljs-keyword">throws</span> IOException &#123;<br>        <span class="hljs-comment">// 输出 Excel</span><br>        EasyExcel.write(response.getOutputStream(), head)<br>                .autoCloseStream(<span class="hljs-literal">false</span>) <span class="hljs-comment">// 不要自动关闭，交给 Servlet 自己处理</span><br>                .registerWriteHandler(<span class="hljs-keyword">new</span> <span class="hljs-title class_">LongestMatchColumnWidthStyleStrategy</span>()) <span class="hljs-comment">// 基于 column 长度，自动适配。最大 255 宽度</span><br>                .sheet(sheetName).doWrite(data);<br>        <span class="hljs-comment">// 设置 header 和 contentType。写在最后的原因是，避免报错时，响应 contentType 已经被修改了</span><br>        response.addHeader(<span class="hljs-string">&quot;Content-Disposition&quot;</span>, <span class="hljs-string">&quot;attachment;filename=&quot;</span> + URLEncoder.encode(filename, <span class="hljs-string">&quot;UTF-8&quot;</span>));<br>        response.setContentType(<span class="hljs-string">&quot;application/vnd.ms-excel;charset=UTF-8&quot;</span>);<br>    &#125;<br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 导出文件时为Writer生成OutputStream</span><br><span class="hljs-comment">     *</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> fileName</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> response</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@return</span></span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> OutputStream <span class="hljs-title function_">getOutputStream</span><span class="hljs-params">(String fileName, HttpServletResponse response)</span> <span class="hljs-keyword">throws</span> Exception &#123;<br>        <span class="hljs-keyword">try</span> &#123;<br>            fileName = URLEncoder.encode(fileName, <span class="hljs-string">&quot;utf-8&quot;</span>);<br>            response.setContentType(<span class="hljs-string">&quot;application/vnd.ms-excel&quot;</span>);<br>            response.setCharacterEncoding(<span class="hljs-string">&quot;utf8&quot;</span>);<br>            response.addHeader(<span class="hljs-string">&quot;Access-Control-Expose-Headers&quot;</span>, <span class="hljs-string">&quot;Content-Disposition&quot;</span>);<br>            <span class="hljs-comment">//文件名固定 filename*=UTF-8&#x27;&#x27;</span><br>            response.setHeader(<span class="hljs-string">&quot;Content-Disposition&quot;</span>, <span class="hljs-string">&quot;attachment; filename*=UTF-8&#x27;&#x27;&quot;</span> + fileName);<br>            response.setHeader(<span class="hljs-string">&quot;Pragma&quot;</span>, <span class="hljs-string">&quot;public&quot;</span>);<br>            response.setHeader(<span class="hljs-string">&quot;Cache-Control&quot;</span>, <span class="hljs-string">&quot;no-store&quot;</span>);<br>            response.addHeader(<span class="hljs-string">&quot;Cache-Control&quot;</span>, <span class="hljs-string">&quot;max-age=0&quot;</span>);<br>            <span class="hljs-keyword">return</span> response.getOutputStream();<br>        &#125; <span class="hljs-keyword">catch</span> (IOException e) &#123;<br>            <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Exception</span>(<span class="hljs-string">&quot;导出excel表格失败!&quot;</span>, e);<br>        &#125;<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">download</span><span class="hljs-params">(String filename, InputStream stream, HttpServletResponse response)</span> <span class="hljs-keyword">throws</span> Exception &#123;<br>        <span class="hljs-keyword">try</span> &#123;<br>            <span class="hljs-comment">// 以流的形式下载文件。</span><br>            response.addHeader(<span class="hljs-string">&quot;Content-Disposition&quot;</span>, <span class="hljs-string">&quot;attachment;filename=&quot;</span> + URLEncoder.encode(filename, <span class="hljs-string">&quot;utf-8&quot;</span>));<br>            response.addHeader(<span class="hljs-string">&quot;Access-Control-Expose-Headers&quot;</span>, <span class="hljs-string">&quot;Content-Disposition&quot;</span>);<br>            <span class="hljs-type">OutputStream</span> <span class="hljs-variable">ot</span> <span class="hljs-operator">=</span> response.getOutputStream();<br>            <span class="hljs-type">BufferedInputStream</span> <span class="hljs-variable">bis</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">BufferedInputStream</span>(stream);<br>            <span class="hljs-type">BufferedOutputStream</span> <span class="hljs-variable">bos</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">BufferedOutputStream</span>(ot);<br>            <span class="hljs-type">byte</span>[] buffer = <span class="hljs-keyword">new</span> <span class="hljs-title class_">byte</span>[<span class="hljs-number">4096</span>];<br>            <span class="hljs-type">int</span> <span class="hljs-variable">length</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>;<br>            <span class="hljs-keyword">while</span> ((length = bis.read(buffer)) &gt; <span class="hljs-number">0</span>) &#123;<br>                bos.write(buffer, <span class="hljs-number">0</span>, length);<br>            &#125;<br>            bos.close();<br>            bis.close();<br>            ot.close();<br>        &#125; <span class="hljs-keyword">catch</span> (IOException ex) &#123;<br>            ex.printStackTrace();<br>            <span class="hljs-keyword">throw</span> ex;<br>        &#125;<br>    &#125;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 定义样式</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@return</span></span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> HorizontalCellStyleStrategy <span class="hljs-title function_">style</span><span class="hljs-params">()</span> &#123;<br>        <span class="hljs-comment">// 头的样式</span><br>        <span class="hljs-type">WriteCellStyle</span> <span class="hljs-variable">headStyle</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">WriteCellStyle</span>();<br>        headStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex());<br>        <span class="hljs-comment">//表头居中</span><br>        headStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);<br>        <span class="hljs-comment">//表头字体</span><br>        <span class="hljs-type">WriteFont</span> <span class="hljs-variable">headFont</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">WriteFont</span>();<br>        headFont.setBold(<span class="hljs-literal">true</span>);<br>        headFont.setFontName(<span class="hljs-string">&quot;宋体&quot;</span>);<br>        headFont.setFontHeightInPoints((<span class="hljs-type">short</span>) <span class="hljs-number">12</span>);<br>        headStyle.setWriteFont(headFont);<br>        headStyle.setWrapped(<span class="hljs-literal">true</span>);<br><br>        <span class="hljs-comment">// 内容的样式</span><br>        <span class="hljs-type">WriteCellStyle</span> <span class="hljs-variable">contentStyle</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">WriteCellStyle</span>();<br>        contentStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND);<br>        contentStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex());<br>        <span class="hljs-comment">//内容字体</span><br>        <span class="hljs-type">WriteFont</span> <span class="hljs-variable">contentWriteFont</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">WriteFont</span>();<br>        contentWriteFont.setFontName(<span class="hljs-string">&quot;宋体&quot;</span>);<br>        contentWriteFont.setFontHeightInPoints((<span class="hljs-type">short</span>) <span class="hljs-number">12</span>);<br>        contentStyle.setWriteFont(contentWriteFont);<br>        contentStyle.setBorderTop(BorderStyle.THIN);<br>        contentStyle.setBorderBottom(BorderStyle.THIN);<br>        contentStyle.setBorderLeft(BorderStyle.THIN);<br>        contentStyle.setBorderRight(BorderStyle.THIN);<br>        contentStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);<br>        <span class="hljs-comment">//垂直居中</span><br>        contentStyle.setVerticalAlignment(VerticalAlignment.CENTER);<br>        contentStyle.setWrapped(<span class="hljs-literal">true</span>);<br>        <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">HorizontalCellStyleStrategy</span>(headStyle, contentStyle);<br>    &#125;<br><br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 自定义不固定一级表头</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> heads</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@return</span></span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> List&lt;List&lt;String&gt;&gt; <span class="hljs-title function_">scoreHead</span><span class="hljs-params">(List&lt;String&gt; heads)</span> &#123;<br>        <span class="hljs-comment">//组装头</span><br>        List&lt;List&lt;String&gt;&gt; list = Lists.newArrayList();<br>        heads.forEach(s-&gt;&#123;<br>            List&lt;String&gt; head = Lists.newArrayList();<br>            head.add(s);<br>            list.add(head);<br>        &#125;);<br>        <span class="hljs-keyword">return</span> list;<br>    &#125;<br><br>    <span class="hljs-comment">//==================================表头不固定情况==========================</span><br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 获取excel对象</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> fileName</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> response</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@return</span></span><br><span class="hljs-comment">     * <span class="hljs-doctag">@throws</span> Exception</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> ExcelWriter <span class="hljs-title function_">writeDynamicHeadExcel</span><span class="hljs-params">(String fileName, HttpServletResponse response)</span> <span class="hljs-keyword">throws</span> Exception &#123;<br>        <span class="hljs-type">ExcelWriter</span> <span class="hljs-variable">excelWriter</span> <span class="hljs-operator">=</span> EasyExcel.write(getOutputStream(fileName, response))<br>                <span class="hljs-comment">// 设置统一的表头、表内容样式</span><br>                .registerWriteHandler(EasyExcelUtils.style())<br>                <span class="hljs-comment">// 设置表头，表内容的行高</span><br>                .registerWriteHandler(<span class="hljs-keyword">new</span> <span class="hljs-title class_">SimpleRowHeightStyleStrategy</span>((<span class="hljs-type">short</span>) <span class="hljs-number">50</span>, (<span class="hljs-type">short</span>) <span class="hljs-number">20</span>))<br>                <span class="hljs-comment">//设置宽度</span><br>                <span class="hljs-comment">//.registerWriteHandler(new Custemhandler())</span><br>                <span class="hljs-comment">//设置内容为文本</span><br>                .registerWriteHandler(<span class="hljs-keyword">new</span> <span class="hljs-title class_">RowWriteHandlerImp</span>())<br>                .build();<br>        <span class="hljs-keyword">return</span> excelWriter;<br>    &#125;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 获取sheet对象</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> sheetNo sheet 序号</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> sheetName sheet 明字</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> head 通过列表指明表头</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@return</span></span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> WriteSheet <span class="hljs-title function_">getExcelSheet</span><span class="hljs-params">(Integer sheetNo ,String sheetName,List&lt;List&lt;String&gt;&gt; head)</span> &#123;<br>        <span class="hljs-type">WriteSheet</span> <span class="hljs-variable">sheet</span> <span class="hljs-operator">=</span> EasyExcel.writerSheet(sheetNo, sheetName)<br>                <span class="hljs-comment">// 自定义多级复杂表头</span><br>                <span class="hljs-comment">//.registerWriteHandler(new RowWriteHandlerImp())</span><br>                .head(head)<br>                .build();<br>        <span class="hljs-keyword">return</span> sheet;<br>    &#125;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 获取sheet对象</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> sheetNo sheet 序号</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> sheetName sheet 明字</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> head 泛型， 保证 head 和 data 类型的一致性</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@return</span></span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> WriteSheet <span class="hljs-title function_">getExcelSheet</span><span class="hljs-params">(Integer sheetNo ,String sheetName, Class&lt;?&gt; head)</span> &#123;<br>        <span class="hljs-type">WriteSheet</span> <span class="hljs-variable">sheet</span> <span class="hljs-operator">=</span> EasyExcel.writerSheet(sheetNo, sheetName)<br>                <span class="hljs-comment">// 自定义多级复杂表头</span><br>                <span class="hljs-comment">//.registerWriteHandler(new RowWriteHandlerImp())</span><br>                .head(head)<br>                .build();<br>        <span class="hljs-keyword">return</span> sheet;<br>    &#125;<br><br>    <span class="hljs-comment">//==================================表头固定情况==========================</span><br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 固定表头 获取excel对象</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> response</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> fileName</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> clazz</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@return</span></span><br><span class="hljs-comment">     * <span class="hljs-doctag">@throws</span> Exception</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> ExcelWriter <span class="hljs-title function_">writeFixedHeadExcel</span><span class="hljs-params">(HttpServletResponse response, String fileName, Class clazz)</span> <span class="hljs-keyword">throws</span> Exception &#123;<br><br>        <span class="hljs-type">OutputStream</span> <span class="hljs-variable">outputStream</span> <span class="hljs-operator">=</span> getOutputStream(fileName, response);<br><br>        <span class="hljs-type">ExcelWriter</span> <span class="hljs-variable">excelWriter</span> <span class="hljs-operator">=</span> EasyExcel.write(outputStream, clazz)<br>                <span class="hljs-comment">// 设置统一的表头、表内容样式</span><br>                .registerWriteHandler(EasyExcelUtils.style())<br>                <span class="hljs-comment">// 设置表头，表内容的行高</span><br>                .registerWriteHandler(<span class="hljs-keyword">new</span> <span class="hljs-title class_">SimpleRowHeightStyleStrategy</span>((<span class="hljs-type">short</span>) <span class="hljs-number">50</span>, (<span class="hljs-type">short</span>) <span class="hljs-number">20</span>))<br>                <span class="hljs-comment">//宽度自适应</span><br>                .registerWriteHandler(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Custemhandler</span>())<br>                .build();<br><br>        <span class="hljs-keyword">return</span> excelWriter;<br>    &#125;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 固定表头，创建sheet对象</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> sheetNo</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> sheetName</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@return</span></span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> WriteSheet <span class="hljs-title function_">getFixedExcelSheet</span><span class="hljs-params">(Integer sheetNo, String sheetName)</span> &#123;<br><br>        <span class="hljs-type">WriteSheet</span> <span class="hljs-variable">writeSheet</span> <span class="hljs-operator">=</span> EasyExcel.writerSheet(sheetNo, sheetName).build();<br><br>        <span class="hljs-keyword">return</span> writeSheet;<br>    &#125;<br><br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 读取</span><br><span class="hljs-comment">     *</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> inputStream</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@return</span></span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> ExcelReader <span class="hljs-title function_">readExcel</span><span class="hljs-params">(InputStream inputStream, AnalysisEventListener listener)</span> &#123;<br>        <span class="hljs-type">ExcelReader</span> <span class="hljs-variable">excelReader</span> <span class="hljs-operator">=</span> EasyExcel.read(inputStream)<br>                .registerReadListener(listener)<br>                .build();<br><br>        <span class="hljs-keyword">return</span> excelReader;<br>    &#125;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 读取</span><br><span class="hljs-comment">     *</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> inputStream</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@return</span></span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> ExcelReader <span class="hljs-title function_">readExcel</span><span class="hljs-params">(InputStream inputStream, Class clazz, AnalysisEventListener listener)</span> &#123;<br>        <span class="hljs-type">ExcelReader</span> <span class="hljs-variable">excelReader</span> <span class="hljs-operator">=</span> EasyExcel.read(inputStream)<br>                .registerReadListener(listener)<br>                .head(clazz)<br>                .build();<br><br>        <span class="hljs-keyword">return</span> excelReader;<br>    &#125;<br><br><br>&#125;<br><br></code></pre></td></tr></table></figure>

<h3 id="简单实现"><a href="#简单实现" class="headerlink" title="简单实现"></a>简单实现</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@ApiOperation(&quot;下载文件测试&quot;)</span><br>    <span class="hljs-meta">@PostMapping(&quot;downloadTest&quot;)</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">downloadTest</span><span class="hljs-params">(HttpServletResponse response)</span> <span class="hljs-keyword">throws</span> Exception &#123;<br>        List&lt;DownloadTestEntity&gt; datas = <span class="hljs-keyword">new</span> <span class="hljs-title class_">ArrayList</span>&lt;&gt;();<br>        <span class="hljs-type">DownloadTestEntity</span> <span class="hljs-variable">dow1</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">DownloadTestEntity</span>();<br>        dow1.setId(<span class="hljs-number">1</span>);<br>        dow1.setUsername(<span class="hljs-string">&quot;username&quot;</span>);<br>        dow1.setName(<span class="hljs-string">&quot;测试1&quot;</span>);<br>        dow1.setGender(<span class="hljs-string">&quot;男&quot;</span>);<br>        dow1.setAge(<span class="hljs-number">10</span>);<br><br>        <span class="hljs-type">DownloadTestEntity</span> <span class="hljs-variable">dow2</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">DownloadTestEntity</span>();<br>        dow2.setId(<span class="hljs-number">1</span>);<br>        dow2.setUsername(<span class="hljs-string">&quot;username&quot;</span>);<br>        dow2.setName(<span class="hljs-string">&quot;测试2&quot;</span>);<br>        dow2.setGender(<span class="hljs-string">&quot;男&quot;</span>);<br>        dow2.setAge(<span class="hljs-number">15</span>);<br><br>        <span class="hljs-type">DownloadTestEntity</span> <span class="hljs-variable">dow3</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">DownloadTestEntity</span>();<br>        dow3.setId(<span class="hljs-number">1</span>);<br>        dow3.setUsername(<span class="hljs-string">&quot;username&quot;</span>);<br>        dow3.setName(<span class="hljs-string">&quot;测试3&quot;</span>);<br>        dow3.setGender(<span class="hljs-string">&quot;男&quot;</span>);<br>        dow3.setAge(<span class="hljs-number">20</span>);<br>        datas.add(dow1);<br>        datas.add(dow2);<br>        datas.add(dow3);<br>        List&lt;List&lt;String&gt;&gt; head = <span class="hljs-keyword">new</span> <span class="hljs-title class_">ArrayList</span>&lt;&gt;();<br>        head.add(Arrays.asList(<span class="hljs-string">&quot;1&quot;</span>));<br>        head.add(Arrays.asList(<span class="hljs-string">&quot;2&quot;</span>));<br><br>        <span class="hljs-type">ExcelWriter</span> <span class="hljs-variable">excelWriter</span> <span class="hljs-operator">=</span> EasyExcelUtils.writeDynamicHeadExcel(<span class="hljs-string">&quot;测试.xls&quot;</span>, response);<br>        <span class="hljs-comment">// 这个采用类来指明头还有使用 List&lt;List&lt;String&gt;&gt; [[1],[2],[3], [4]] 指明四列表头</span><br>        <span class="hljs-type">WriteSheet</span> <span class="hljs-variable">sheet1</span> <span class="hljs-operator">=</span> EasyExcelUtils.getExcelSheet(<span class="hljs-number">1</span>, <span class="hljs-string">&quot;测试1&quot;</span>, DownloadTestEntity.class);<br>        <span class="hljs-type">WriteSheet</span> <span class="hljs-variable">sheet2</span> <span class="hljs-operator">=</span> EasyExcelUtils.getExcelSheet(<span class="hljs-number">2</span>, <span class="hljs-string">&quot;测试2&quot;</span>, DownloadTestEntity.class);<br>        excelWriter.write(datas, sheet2);<br>        excelWriter.write(datas, sheet1);<br>        excelWriter.finish();<br>    &#125;<br></code></pre></td></tr></table></figure>

                
              </div>
            
            <hr/>
            <div>
              <div class="post-metas my-3">
  
    <div class="post-meta mr-3 d-flex align-items-center">
      <i class="iconfont icon-category"></i>
      

<span class="category-chains">
  
  
    
      <span class="category-chain">
        
  <a href="/categories/%E4%B8%8B%E8%BD%BDexcel/" class="category-chain-item">下载excel</a>
  
  

      </span>
    
  
</span>

    </div>
  
  
    <div class="post-meta">
      <i class="iconfont icon-tags"></i>
      
        <a href="/tags/springboot/">#springboot</a>
      
    </div>
  
</div>


              
  

  <div class="license-box my-3">
    <div class="license-title">
      <div>java下载多sheet</div>
      <div>https://sweetshance.github.io/2024/12/13/springBoot学习/java下载多sheet/</div>
    </div>
    <div class="license-meta">
      
        <div class="license-meta-item">
          <div>作者</div>
          <div>ShanCe</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>发布于</div>
          <div>2024年12月13日</div>
        </div>
      
      
      
    </div>
    <div class="license-icon iconfont"></div>
  </div>



              
                <div class="post-prevnext my-3">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2024/12/19/python%E7%9B%B8%E5%85%B3%E7%9F%A5%E8%AF%86%E7%82%B9/pandas%E5%88%86%E7%BB%84%E5%B0%86%E5%A4%9A%E8%A1%8C%E5%A4%9A%E5%88%97%E6%94%BE%E5%9C%A8%E4%B8%80%E8%B5%B7/" title="pandas分组将多行多列放在一起">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">pandas分组将多行多列放在一起</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2024/11/26/docker/docker%E9%83%A8%E7%BD%B2jenkins/" title="docker部署jenkins">
                        <span class="hidden-mobile">docker部署jenkins</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>

    <div class="side-col d-none d-lg-block col-lg-2">
      
  <aside class="sidebar" style="margin-left: -1rem">
    <div id="toc">
  <p class="toc-header">
    <i class="iconfont icon-list"></i>
    <span>目录</span>
  </p>
  <div class="toc-body" id="toc-body"></div>
</div>



  </aside>


    </div>
  </div>
</div>





  



  



  



  



  







    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v" for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>

    

    
  </main>

  <footer>
    <div class="footer-inner">
  
    <div class="footer-content">
       <a href="http://82.157.172.231/" target="_blank" rel="nofollow noopener"><span>无聊</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>ShanCe</span></a> 
    </div>
  
  
    <div class="statistics">
  
  

  
    
      <span id="busuanzi_container_site_pv" style="display: none">
        总访问量 
        <span id="busuanzi_value_site_pv"></span>
         次
      </span>
    
    
      <span id="busuanzi_container_site_uv" style="display: none">
        总访客数 
        <span id="busuanzi_value_site_uv"></span>
         人
      </span>
    
    
  
</div>

  
  
  
</div>

  </footer>

  <!-- Scripts -->
  
  <script  src="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://lib.baomitu.com/jquery/3.6.0/jquery.min.js" ></script>
<script  src="https://lib.baomitu.com/twitter-bootstrap/4.6.1/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>


  <script  src="https://lib.baomitu.com/typed.js/2.0.12/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var subtitle = document.getElementById('subtitle');
      if (!subtitle || !typing) {
        return;
      }
      var text = subtitle.getAttribute('data-typed-text');
      
        typing(text);
      
    })(window, document);
  </script>




  
    <script  src="/js/img-lazyload.js" ></script>
  




  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/tocbot/4.18.2/tocbot.min.js', function() {
    var toc = jQuery('#toc');
    if (toc.length === 0 || !window.tocbot) { return; }
    var boardCtn = jQuery('#board-ctn');
    var boardTop = boardCtn.offset().top;

    window.tocbot.init(Object.assign({
      tocSelector     : '#toc-body',
      contentSelector : '.markdown-body',
      linkClass       : 'tocbot-link',
      activeLinkClass : 'tocbot-active-link',
      listClass       : 'tocbot-list',
      isCollapsedClass: 'tocbot-is-collapsed',
      collapsibleClass: 'tocbot-is-collapsible',
      scrollSmooth    : true,
      includeTitleTags: true,
      headingsOffset  : -boardTop,
    }, CONFIG.toc));
    if (toc.find('.toc-list-item').length > 0) {
      toc.css('visibility', 'visible');
    }

    Fluid.events.registerRefreshCallback(function() {
      if ('tocbot' in window) {
        tocbot.refresh();
        var toc = jQuery('#toc');
        if (toc.length === 0 || !tocbot) {
          return;
        }
        if (toc.find('.toc-list-item').length > 0) {
          toc.css('visibility', 'visible');
        }
      }
    });
  });
</script>


  <script src=https://lib.baomitu.com/clipboard.js/2.0.11/clipboard.min.js></script>

  <script>Fluid.plugins.codeWidget();</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/anchor-js/4.3.1/anchor.min.js', function() {
    window.anchors.options = {
      placement: CONFIG.anchorjs.placement,
      visible  : CONFIG.anchorjs.visible
    };
    if (CONFIG.anchorjs.icon) {
      window.anchors.options.icon = CONFIG.anchorjs.icon;
    }
    var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
    var res = [];
    for (var item of el) {
      res.push('.markdown-body > ' + item.trim());
    }
    if (CONFIG.anchorjs.placement === 'left') {
      window.anchors.options.class = 'anchorjs-link-left';
    }
    window.anchors.add(res.join(', '));

    Fluid.events.registerRefreshCallback(function() {
      if ('anchors' in window) {
        anchors.removeAll();
        var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
        var res = [];
        for (var item of el) {
          res.push('.markdown-body > ' + item.trim());
        }
        if (CONFIG.anchorjs.placement === 'left') {
          anchors.options.class = 'anchorjs-link-left';
        }
        anchors.add(res.join(', '));
      }
    });
  });
</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.js', function() {
    Fluid.plugins.fancyBox();
  });
</script>


  <script>Fluid.plugins.imageCaption();</script>

  <script  src="/js/local-search.js" ></script>

  <script defer src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" ></script>





<!-- 主题的启动项，将它保持在最底部 -->
<!-- the boot of the theme, keep it at the bottom -->
<script  src="/js/boot.js" ></script>


  

  <noscript>
    <div class="noscript-warning">博客在允许 JavaScript 运行的环境下浏览效果更佳</div>
  </noscript>
</body>
</html>
